package com.wsd.jing.controller.system;

import com.wsd.jing.annotation.ControllerLog;
import com.wsd.jing.entity.Role;
import com.wsd.jing.request.CommonRequest;
import com.wsd.jing.request.CommonResponse;
import com.wsd.jing.service.IRoleService;
import com.wsd.jing.validator.group.InsertGroup;
import com.wsd.jing.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.controller
 * @className: RoleController
 * @description: 角色前端控制器
 * @createDate 2022-11-29 04:06
 * @copyright 2022-11-29 04:06 www.tom.com Inc. All rights reserved.
 */
@Api(tags = "角色")
@Validated
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/rest/role")
public class RoleController {

    private final IRoleService roleService;

    @ApiOperation(value = "新增")
    @PostMapping()
    @ControllerLog(action = "新增角色", message = "新增角色失败")
    // @SaCheckPermission("role:add")
    public CommonResponse insertRole(@Validated(InsertGroup.class) @RequestBody Role role) {
        return CommonResponse.success(roleService.insertRole(role), "新增成功");
    }

    @ApiOperation(value = "修改")
    @PutMapping()
    @ControllerLog(action = "修改角色", message = "修改角色失败")
    // @SaCheckPermission("role:update")
    public CommonResponse updateRole(@Validated(UpdateGroup.class) @RequestBody Role role) {
        return CommonResponse.success(roleService.updateRole(role), "修改成功");
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "角色id", paramType = "path", dataType = "Integer", required = true)
    @DeleteMapping("/{id}")
    @ControllerLog(action = "删除角色", message = "删除角色失败")
    // @SaCheckPermission("role:delete")
    public CommonResponse deleteRole(@PathVariable("id") Integer id) {
        return CommonResponse.success(roleService.deleteRole(id), "删除成功");
    }

    @ApiOperation(value = "批量删除")
    @ApiImplicitParam(name = "ids", value = "角色id逗号分割字符", paramType = "query", dataType = "String", required = true)
    @DeleteMapping("")
    @ControllerLog(action = "批量删除角色", message = "批量删除角色失败")
    // @SaCheckPermission("role:batch:delete")
    public CommonResponse deleteRoles(@RequestParam("ids") String ids) {
        return CommonResponse.success(roleService.deleteRoles(ids), "批量删除成功");
    }

    @ApiOperation(value = "查询")
    @ApiImplicitParam(name = "id", value = "角色id", paramType = "path", dataType = "Integer", required = true)
    @GetMapping("/{id}")
    @ControllerLog(action = "查询角色", message = "查询角色失败")
    // @SaCheckPermission("role:get")
    public CommonResponse getRole(@PathVariable("id") Integer id) {
        return CommonResponse.success(roleService.getRole(id), "查询成功");
    }

    @ApiOperation(value = "查询列表")
    @GetMapping("/list")
    @ControllerLog(action = "查询角色列表", message = "查询角色列表失败")
    // @SaCheckPermission("role:list")
    public CommonResponse listRoles(CommonRequest commonRequest, Role role) {
        return CommonResponse.success(roleService.listRoles(commonRequest, role), "查询成功");
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/pages")
    @ControllerLog(action = "分页查询角色", message = "分页查询角色失败")
    // @SaCheckPermission("role:page")
    public CommonResponse listPageRoles(CommonRequest commonRequest, Role role) {
        return CommonResponse.success(roleService.listPageRoles(commonRequest, role), "查询成功");
    }
}
